package com.game;

import java.awt.*;
import java.util.ArrayList;

public class EnemyBullet extends Bullet{

    public EnemyBullet(String img, int x, int y, GamePanel gamePanel, Direction direction) {
        super(img, x, y, gamePanel, direction);
    }

    /*
     * 敌方子弹撞击我方坦克
     * */
    public void hitPlayer() {
        ArrayList<Tank> players = this.getGamePanel().playerList;
        for (Tank player : players) {
            if (this.gerRec().intersects(player.gerRec())) {
                //添加爆炸动画的位置
                getGamePanel().explodeList.add(new Explode("image/cxkboom2.gif", player.x,player.y,getGamePanel()));
                this.getGamePanel().playerList.remove(player);
                this.getGamePanel().removeList2.add(this);
                //被子弹击中 状态改为false
                player.alive = false;
                Music.hitTankPlay();
                break;
            }
        }
    }

    /*
    * 敌方子弹击中墙
    * */
    @Override
    public void hitWall() {
        ArrayList<Wall> wallLists = getGamePanel().wallList;
        for (Wall wallList : wallLists) {
            if (this.gerRec().intersects(wallList.gerRec())){
                getGamePanel().wallList.remove(wallList);
                getGamePanel().removeList2.add(this);
                break;
            }
        }
    }

    /*
    * 敌方子弹击中Fe墙
    * */
    @Override
    public void hitFe() {
        Rectangle next = this.gerRec();
        ArrayList<Fe> fes = getGamePanel().feList;
        for (Fe fe : fes) {
            if (fe.gerRec().intersects(next)) {
                getGamePanel().removeList2.add(this);
                break;
            }
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        g.drawImage(getImg(),x,y,getWidth(),getHeight(),null);
        //在绘制方法中加入子弹移动
        shoot();
        hitPlayer();
        hitWall();
        hitFe();
    }

    @Override
    public Rectangle gerRec() {
        return new Rectangle(x,y,getWidth(),getHeight());
    }
}
